//=============================================================================
/**
 *
 *	@file		museum_bg.c
 *	@brief		ポケスロンミュージアム BG管理モジュール
 *	@author		hosaka genya
 *	@data		2008.08.20
 *
 */
//=============================================================================
#include "common.h"
#include "gflib/touchpanel.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/pm_str.h"
#include "system/wordset.h"
#include "system/msgdata_util.h"
#include "system/fontproc.h"
#include "msgdata/msg.naix"

#include "application/app_tool.h"

#include "museum_util.h"
#include "museum_bg.h"

// #define DEBUG_FONT_BACK // フォントの背景をデバッグ用にする
//=============================================================================
/**
 *								定数定義
 */
//=============================================================================
enum
{
	STR_BUF_SIZE	= 1024,		///< 文字バッファサイズ
};

//-----------------------------------------------------------------------------
// GF_PRINT_COLOR
//-----------------------------------------------------------------------------
// -- 透明色 --
#define COL_NL (GF_PRINTCOLOR_MAKE(0, 0, 0))
// -- 白 --
#define	COL_WW (GF_PRINTCOLOR_MAKE(15, 15, 0))
// -- 黒 --
#define COL_BW (GF_PRINTCOLOR_MAKE(FBMP_COL_BLACK, FBMP_COL_BLK_SDW, FBMP_COL_NULL))

//=============================================================================
/**
 *								構造体定義
 */
//=============================================================================

//=============================================================================
/**
 *							プロトタイプ宣言
 */
//=============================================================================
// -- prototype --
static void bmpwin_print_core( MUSEUM_MSG_WORK* p_msg, u32 win_idx, u32 msg_idx, BOOL b_center, u8 add_x, u8 add_y, FONT_TYPE font, u8 back_col );
static void bmpwin_clear( MUSEUM_MSG_WORK* p_msg, u32 win_idx );

//=============================================================================
/**
 *								外部公開関数
 */
//=============================================================================

//-----------------------------------------------------------------------------
/**
 *	@brief
 *
 *	@param	HEAPID heapID
 *	@param	win_max
 *	@param	BMPWIN_DAT bmpwin_list[]
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
MUSEUM_MSG_WORK* MUSEUM_BMPWIN_Create( HEAPID heapID, GF_BGL_INI* p_bgl, int win_max, const BMPWIN_DAT bmpwin_list[], u32 msg_arc_idx )
{
	int i;
	MUSEUM_MSG_WORK* p_msg;

	// タッチフォントをロード
	FontProc_LoadFont( FONT_TOUCH, heapID );

	p_msg = sys_AllocMemory( heapID, sizeof( MUSEUM_MSG_WORK ) );
	MI_CpuClear8( p_msg, sizeof( MUSEUM_MSG_WORK ) );

	// winを動的に確保
	p_msg->win = sys_AllocMemory( heapID, sizeof(GF_BGL_BMPWIN)*win_max );
	MI_CpuClear8( p_msg->win, sizeof(GF_BGL_BMPWIN)*win_max );

	//フォント用パレットセット
	SystemFontPaletteLoad(	PALTYPE_MAIN_BG,	FONT_TALK_PAL*0x20,	heapID );
	SystemFontPaletteLoad(	PALTYPE_SUB_BG,		FONT_TALK_PAL*0x20,	heapID );

	for( i=0; i<win_max; i++ )
	{
		GF_BGL_BmpWinAddEx( p_bgl, &p_msg->win[i], &bmpwin_list[i] );
		GF_BGL_BmpWinDataFill( &p_msg->win[i], COL_NL );
		GF_BGL_BmpWinClearScrn( &p_msg->win[i] );
	}

	// 文字列関係
	p_msg->p_mm_mons = MSGMAN_Create( MSGMAN_TYPE_DIRECT, ARC_MSG, NARC_msg_monsname_dat, heapID );
	p_msg->p_mm = MSGMAN_Create( MSGMAN_TYPE_DIRECT, ARC_MSG, msg_arc_idx, heapID );
	p_msg->p_wset = WORDSET_Create( heapID );
	p_msg->p_strbuf = STRBUF_Create( STR_BUF_SIZE, heapID );
	p_msg->heapID = heapID;

	p_msg->win_max = win_max;

	return p_msg;
}

//-----------------------------------------------------------------------------
/**
 *	@brief	BMPWIN 開放
 *
 *	@param	MUSEUM_COURSE_WORK* wk
 *
 *	@retval	none
 */
//-----------------------------------------------------------------------------
void MUSEUM_BMPWIN_Delete( MUSEUM_MSG_WORK* p_msg, int win_max )
{
	int i;

	FontProc_UnloadFont( FONT_TOUCH );

	STRBUF_Delete( p_msg->p_strbuf );

	WORDSET_Delete( p_msg->p_wset );
	MSGMAN_Delete( p_msg->p_mm_mons );
	MSGMAN_Delete( p_msg->p_mm );

	for( i=0; i<win_max; i++ )
	{
		GF_BGL_BmpWinOff( &p_msg->win[i] );
		GF_BGL_BmpWinDel( &p_msg->win[i] );
	}

	sys_FreeMemoryEz( p_msg->win );
	sys_FreeMemoryEz( p_msg );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	指定したBMPWINに文字列を展開
 *
 *	@param	MUSEUM_COURSE_WORK* wk
 *	@param	win_idx
 *	@param	msg_idx
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void MUSEUM_BMPWIN_Print( MUSEUM_MSG_WORK* p_msg, u32 win_idx, u32 msg_idx, BOOL b_center )
{
	MUSEUM_BMPWIN_PrintEx( p_msg, win_idx, msg_idx, b_center, 0, FONT_SYSTEM );
}

// フォントタイプ、Y座標指定版
void MUSEUM_BMPWIN_PrintEx( MUSEUM_MSG_WORK* p_msg, u32 win_idx, u32 msg_idx, BOOL b_center, u8 add_y, FONT_TYPE font )
{
	bmpwin_print_core( p_msg, win_idx, msg_idx, b_center, 0, add_y, font, 0 );
}

// オフセット指定版
void MUSEUM_BMPWIN_PrintAndSetOfs( MUSEUM_MSG_WORK* p_msg, u32 win_idx, u32 msg_idx, u8 add_x, u8 add_y )
{
	bmpwin_print_core( p_msg, win_idx, msg_idx, FALSE, add_x, add_y, FONT_SYSTEM, 0 );
}

// 背景色指定版
void MUSEUM_BMPWIN_PrintAndSelectBackColor( MUSEUM_MSG_WORK* p_msg, u32 win_idx, u32 msg_idx, u8 back_col )
{
	bmpwin_print_core( p_msg, win_idx, msg_idx, FALSE, 0, 0, FONT_SYSTEM, back_col );
}

// 内部共通処理
static void bmpwin_print_core( MUSEUM_MSG_WORK* p_msg, u32 win_idx, u32 msg_idx, BOOL b_center, u8 add_x, u8 add_y, FONT_TYPE font, u8 back_col )
{
	u8 width = 0;
	STRBUF* buf;

	GF_ASSERT( win_idx < p_msg->win_max );

	buf = MSGDAT_UTIL_AllocExpandString( p_msg->p_wset, p_msg->p_mm, msg_idx, p_msg->heapID );
//	MSGMAN_GetString( p_msg->p_mm, msg_idx, p_msg->p_strbuf );

	// センタリング処理
	if( b_center )
	{
		width = GF_BGL_BmpWinGet_SizeX( &p_msg->win[ win_idx ] ) * 8;
		width -= FontProc_GetPrintStrWidth( font, buf, 0 );
		width >>= 1;
	}

	GF_BGL_BmpWinDataFill( &p_msg->win[ win_idx ], back_col );
	GF_STR_PrintColor( &p_msg->win[ win_idx ], font, buf, width + add_x, add_y, MSG_NO_PUT, COL_BW, NULL );
	GF_BGL_BmpWinOnVReq( &p_msg->win[ win_idx ] );

	STRBUF_Delete( buf );
}


//-----------------------------------------------------------------------------
/**
 *	@brief	BMPWINに数値を表示（数字１スロット限定版）
 *
 *	@param	MUSEUM_MSG_WORK* p_msg
 *	@param	win_idx
 *	@param	msg_idx
 *	@param	num
 *	@param	figre
 *	@param	buf_idx
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void MUSEUM_BMPWIN_PrintNum( MUSEUM_MSG_WORK* p_msg, u32 win_idx, u32 msg_idx, s32 num, u32 figre, u32 buf_idx )
{
	STRBUF* buf;

	WORDSET_RegisterNumber( p_msg->p_wset, buf_idx, num, figre, NUMBER_DISPTYPE_SPACE, NUMBER_CODETYPE_DEFAULT );

	buf = MSGMAN_AllocString( p_msg->p_mm, msg_idx );
	WORDSET_ExpandStr( p_msg->p_wset, p_msg->p_strbuf, buf );

	bmpwin_clear( p_msg, win_idx );
	GF_STR_PrintColor( &p_msg->win[ win_idx ], FONT_SYSTEM, p_msg->p_strbuf, 0, 0, MSG_NO_PUT, COL_BW, NULL );
	GF_BGL_BmpWinOnVReq( &p_msg->win[ win_idx ] );

	STRBUF_Delete( buf );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	単語・数値をRegistしたメッセージを表示
 *
 *	@param	MUSEUM_MSG_WORK* p_msg
 *	@param	win_idx
 *	@param	msg_idx
 *	@param	buf_idx
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void MUSEUM_BMPWIN_PrintRegStr( MUSEUM_MSG_WORK* p_msg, u32 win_idx, u32 msg_idx )
{
	MUSEUM_BMPWIN_PrintRegStrWithPos( p_msg, win_idx, msg_idx, 0, 0 );
}

// 座標指定版
void MUSEUM_BMPWIN_PrintRegStrWithPos( MUSEUM_MSG_WORK* p_msg, u32 win_idx, u32 msg_idx, u8 px, u8 py )
{
	STRBUF* buf;

	buf = MSGMAN_AllocString( p_msg->p_mm, msg_idx );
	WORDSET_ExpandStr( p_msg->p_wset, p_msg->p_strbuf, buf );

	bmpwin_clear( p_msg, win_idx );
	GF_STR_PrintColor( &p_msg->win[ win_idx ], FONT_SYSTEM, p_msg->p_strbuf, px, py, MSG_NO_PUT, COL_BW, NULL );
	GF_BGL_BmpWinOnVReq( &p_msg->win[ win_idx ] );

	STRBUF_Delete( buf );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	WORDSETに数字を登録
 *
 *	@param	MUSEUM_MSG_WORK* p_msg
 *	@param	num
 *	@param	figre
 *	@param	buf_idx
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void MUSEUM_BMPWIN_RegistNum( MUSEUM_MSG_WORK* p_msg, s32 num, u32 figre, u32 buf_idx )
{
	WORDSET_RegisterNumber( p_msg->p_wset, buf_idx, num, figre, NUMBER_DISPTYPE_SPACE, NUMBER_CODETYPE_DEFAULT );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	プレイヤー名を登録
 *
 *	@param	MUSEUM_MSG_WORK* p_msg
 *	@param	win_idx
 *	@param	buf_idx
 *	@param	MYSTATUS*  p_st
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void MUSEUM_BMPWIN_PrintPlayerName( MUSEUM_MSG_WORK* p_msg, u32 win_idx, u32 msg_idx, u32 buf_idx, const MYSTATUS*  p_st )
{
	STRBUF* buf;

	WORDSET_RegisterPlayerName( p_msg->p_wset, buf_idx, p_st );
	buf = MSGMAN_AllocString( p_msg->p_mm, msg_idx );
	WORDSET_ExpandStr( p_msg->p_wset, p_msg->p_strbuf, buf );

	bmpwin_clear( p_msg, win_idx );

	GF_STR_PrintColor( &p_msg->win[ win_idx ], FONT_SYSTEM, p_msg->p_strbuf, 0, 0, MSG_NO_PUT, COL_BW, NULL );
	GF_BGL_BmpWinOnVReq( &p_msg->win[ win_idx ] );

	STRBUF_Delete( buf );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	ポケモンの種族名を表示
 *
 *	@param	MUSEUM_MSG_WORK* p_msg
 *	@param	win_idx
 *	@param	monsno
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void MUSEUM_BMPWIN_PrintPokemonName( MUSEUM_MSG_WORK* p_msg, u32 win_idx, u16 monsno )
{
	MSGDATA_MANAGER*	p_mm_mons = p_msg->p_mm_mons;

	GF_ASSERT( monsno <= MONSNO_END );

	MSGMAN_GetString( p_mm_mons,  monsno, p_msg->p_strbuf );
	GF_BGL_BmpWinDataFill( &p_msg->win[ win_idx ], COL_NL );
	GF_STR_PrintColor( &p_msg->win[ win_idx ], FONT_SYSTEM, p_msg->p_strbuf, 0, 0, MSG_NO_PUT, COL_BW, NULL );
	GF_BGL_BmpWinOnVReq( &p_msg->win[ win_idx ] );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	BMPWINをクリア＆転送リクエスト
 *
 *	@param	MUSEUM_MSG_WORK* p_msg
 *	@param	win_idx
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void MUSEUM_BMPWIN_ClearAndTransReq( MUSEUM_MSG_WORK* p_msg, u32 win_idx )
{
	bmpwin_clear( p_msg, win_idx );
	GF_BGL_BmpWinOnVReq( &p_msg->win[ win_idx ] );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	STRBUFを直接描画
 *
 *	@param	MUSEUM_MSG_WORK* p_msg
 *	@param	win_idx
 *	@param	STRBUF* p_strbuf
 *	@param	px
 *	@param	py
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void MUSEUM_BMPWIN_PrintStrbuf( MUSEUM_MSG_WORK* p_msg, u32 win_idx, const STRBUF* p_strbuf, u8 px, u8 py )
{
	bmpwin_clear( p_msg, win_idx );
	GF_STR_PrintColor( &p_msg->win[ win_idx ], FONT_SYSTEM, p_strbuf, px, py, MSG_NO_PUT, COL_BW, NULL );
	GF_BGL_BmpWinOnVReq( &p_msg->win[ win_idx ] );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	スクリーンクリア＆転送リクエスト
 *
 *	@param	MUSEUM_MSG_WORK* p_msg
 *	@param	win_idx
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void MUSEUM_BMPWIN_OffVReq( MUSEUM_MSG_WORK* p_msg, u32 win_idx )
{
	GF_BGL_BmpWinOffVReq( &p_msg->win[ win_idx ] );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	BMPWINのY座標を設定
 *
 *	@param	MUSEUM_MSG_WORK* p_msg
 *	@param	win_idx
 *	@param	pos_y
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void MUSEUM_BMPWIN_SetPosY( MUSEUM_MSG_WORK* p_msg, u32 win_idx, u8 pos_y )
{
	GF_BGL_BmpWinSet_PosY( &p_msg->win[ win_idx ], pos_y );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	ワードセットのポインタを取得
 *
 *	@param	MUSEUM_MSG_WORK* p_msg
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
WORDSET* MUSEUM_BMPWIN_GetWordset( MUSEUM_MSG_WORK* p_msg )
{
	GF_ASSERT(p_msg);
	GF_ASSERT(p_msg->p_wset);

	return p_msg->p_wset;
}

//=============================================================================
/**
 *								static関数
 */
//=============================================================================

//-----------------------------------------------------------------------------
/**
 *	@brief	BMPWINをクリア（転送は行わない）
 *
 *	@param	MUSEUM_COURSE_WORK* wk
 *	@param	win_idx
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void bmpwin_clear( MUSEUM_MSG_WORK* p_msg, u32 win_idx )
{
#ifdef DEBUG_FONT_BACK
	GF_BGL_BmpWinDataFill( &p_msg->win[ win_idx ], GF_PRINTCOLOR_MAKE(15,15,9) );
#else
	GF_BGL_BmpWinDataFill( &p_msg->win[ win_idx ], COL_NL );
#endif

}


